package cn.zptc.web;

import cn.zptc.dao.impl.FavoriteDaoImpl;
import cn.zptc.entity.FavoriteCategory;
import cn.zptc.service.FavoriteService;
import cn.zptc.service.impl.FavoriteServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/client/getMyFavoriteCategorys")
public class GetMyFavoriteCategorysServlet extends HttpServlet {

    private FavoriteService favoriteService;

    @Override
    public void init() throws ServletException {
        super.init();

        favoriteService = new FavoriteServiceImpl(new FavoriteDaoImpl());
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //todo：先假定用户的id为i，以后从session取user对象进而获取id
        try {
            List<FavoriteCategory> linkCategories= favoriteService.getMyFavoriteCategorys(1);
            String jsonString = JSON.toJSONString(linkCategories);
            resp.setContentType("text/json;charset=UTF-8");
            resp.getWriter().write(jsonString);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
